package com.alipay.api.test.fsf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.csvreader.CsvReader;

public class AlipayBillShaX {
	// 初始化实例请求对象
	// public static String APP_ID = "2016091300501441";
	// public static String APP_PRIVATE_KEY =
	 //"MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCbIE5coYixmwd8Kq8cZ/veQbdVba8cQF0ac9XcekJvAW1qoxO86EHzYCaTSHpZeILub5N3IKclm1qP29ELqb/9TeHzDBAsCHBgW2Jp8YEuAK5jvG+fheakp2uJ+L8yxptdu2WdJKc02U9EtA0N81T4Gy4JtMaa0NMvu+1tYy8QhpNA7GlBmxf2v6zws6LEyv/8jWpXOw+NI38Wt8cObdSfbdNXi8gsXkKb3yb5rYrAInUagHE+suN8oycYAi+VPaL3DeHJb81cSUcAQP38/9DDENDF3duOp8DyBo4Qp/nhFClOZOZoBhYnqeKaFEZ2qeKoVr1M/DEe3evpYp8RfaAxAgMBAAECggEBAJG3CgTXM6jpoGFRUwYS4zOGdWlIy7l0+D/V2WjuayMMqzQFFZBYd0Le6b1l0o/itNK2ekXnpTqdMLd7W2ViQdb15q3AghxqnOzWSp6L3bVRrH5BTnUBeYLnRQvoXembR+wHEM9I08u/6Ub2/mBBV2prVZaGehyI1ie7lq4vAdG5rdgHr+QrAcv9+rvMIjwMRR83a7FawXG1Ivp1dpCZhhYZF9oMuDE1O15xOhCImFd/xusa/hppFxcwnAOjKwWqIjYbEfj58W/zTjm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YZaEm+czS4YKbQOK5p/k0ZUGW78jX5zfRzBHCjSCKSxbKw2RpmwcHbx6rZKStMB3Ke3MDPx/p8kayKDVKXfYH4b9ZwdMRPLrxbEiH727k4DO6K5LR6++X6nYRDPeO007uMcBJ+Lfof2s1o0Kfi5O0Mt9Z1IwoKJTFyh6OMtmhJwWrR6W1hRToMGusv1PP9rc7jUHBgPZ1zLEY1paDwEL0xXSzscN/G0Zgl7bAclW2KP5IdpavuWGes64zI7T5HU4WwpCAJMD2VsFEQUwU/qifrW0mtNT6pVq7dPTQIDAQAB";
	public static String APP_ID = "2016091200495918";
	public static String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDQ/jy2eq1kBc7RnWi/nG8HPeUkITSfeqr76+DsICbIq/NX3+hUvH0bgwxkvYwpcFEAFT7ss51O+bbvW7Ial8EXKVnVMCzZpM6xewpeJ/4VVv2FThVURUZOZ/3FGWWvbF7v2qPDVXDMW8/geLMdEnXzqvlpB5aEkIqNhldwAzjCw7kPKkeeajTgx/x5i/mgID67tiGBwtmqnjjwL9kcz/XseyMYZcMzU9jp2K45oIMzmHt+cVT9wEQX8zYM1PhczNoXxv91kbjBqm9+3ere3COm0SojJ6YQKGopKjHOYGgubHmDgeKXtm3/Egpv589w/JRjHkB96DA6h+FBAR5I4jqZAgMBAAECggEBAJPzV5GrOTJ09hvofm4fVW5f/+Za54D6+y6KptkiTbKEICoWZcgPA9ZFKLHsidwQGhef8sLoyLVPDJVYabZvtg8qg+DrUOTmyjhUrVtInpQG2ETVqT3WKNZ6wekVeKn+YxsTwhkXVFonqPr1F3++QOg2IoibCZBhLuTPtPikHzuowWFNgVqVDMKbpl5ppP8DpUaColro/ZGD5CdwTudeacrG9XElMmeOYtwt+bH6pdx8P5XsSSyNspPFtJaJY6C2Qu0zLzNDrCru50GwrFLflwuA1lYTWxyuKON2ZDb7qsO6+V7G/zaetIv5QEwR3RcPxq1gRO1KsVQnY95Lf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wMKbf3/on1v7DbQrDFH2b1cM4i0R4ChCYvQ9jZKYWJKnYUc0diC8NLSBNMTitYpKZ/fO1MHWwVx0/lb1ALSRoJFclylY04wWVSvdmBBpn/0P5eHA+L+jo+i96mJzVJMizkDlQ0x5TWwisVhUJaDoNKxe9ikUksQP7sAnoqmotOEI7YAIkj4gG2l3Azj4Kn97YTg7Jw5UGLHdJXZ+X/NV0hEzPzXv1ImwIDAQAB";
	public static AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "GBK", ALIPAY_PUBLIC_KEY, "RSA2");

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// 设置下载后生成Zip目录
		long time = new Date().getTime();
		String path = "F:\\fund_bill_" + time + ".zip";
		bill_download_url(path);// 下载对账单
		ZipFileHandler zipFileHandler = new ZipFileHandler();
		zipFileHandler.unZip(path, "F:\\fund_bill_" + time);
		
		File[] fs = new File("F:\\fund_bill_" + time).listFiles();
		String inString = null;
		for (File file : fs) {
			//if (file.getAbsolutePath().contains("汇总")) {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
				CsvReader creader = new CsvReader(reader, ',');
				while (creader.readRecord()) {
					inString = creader.getRawRecord();// 读取一行数据
					//System.out.println(inString);
					if(!inString.startsWith("#")){
						String[] split = inString.split(",");
						for (String string : split) {
							System.out.println(string);
						}
					}
				}
				creader.close();
				reader.close();
			//}
		}
		
	}

	/**
	 * 指定下载目录,下载账单zip文件
	 * 
	 * @author fangshufeng 2018年2月11日
	 * @throws Exception 
	 */
	public static void bill_download_url(String filePath) throws Exception {

		AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
		request.setBizContent("{" + "    \"bill_type\":\"trade\"," + "    \"bill_date\":\"2016-07-11\"" + "  }");
		AlipayDataDataserviceBillDownloadurlQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
			System.out.println(response.getBillDownloadUrl());

		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		if (response.isSuccess()) {
			// 将接口返回的对账单下载地址传入urlStr
			String urlStr = response.getBillDownloadUrl();
			// 指定希望保存的文件路径
			
			URL url = null;
			HttpURLConnection httpUrlConnection = null;
			InputStream fis = null;
			FileOutputStream fos = null;
			try {
				url = new URL(urlStr);
				httpUrlConnection = (HttpURLConnection) url.openConnection();
				httpUrlConnection.setConnectTimeout(5 * 1000);
				httpUrlConnection.setDoInput(true);
				httpUrlConnection.setDoOutput(true);
				httpUrlConnection.setUseCaches(false);
				httpUrlConnection.setRequestMethod("GET");
				httpUrlConnection.setRequestProperty("CHARSET", "GBK");
				httpUrlConnection.connect();
				fis = httpUrlConnection.getInputStream();
				byte[] temp = new byte[1024];
				int b;
				fos = new FileOutputStream(new File(filePath));
				while ((b = fis.read(temp)) != -1) {
					fos.write(temp, 0, b);
					fos.flush();
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (fis != null)
						fis.close();
					if (fos != null)
						fos.close();
					if (httpUrlConnection != null)
						httpUrlConnection.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}